/**
 * Created on May 16, 2007
 */
package jvs.vfs.tools;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 * @author qiangli
 * 
 */
public class LoginDialog extends BasicDialog {

	private static final long serialVersionUID = 1L;

	private JTextField email;

	private JPasswordField password;

	private JLabel email_label, password_label;

	private String passwordLabelText;

	private String passwordLabelErrorText;

	private String emailLabelText;

	private String emailLabelErrorText;

	private class MyFocusListener implements FocusListener {
		public void focusGained(FocusEvent e) {
			Object which = e.getSource();
			if (which == password) {

				password_label.setText(passwordLabelText);
			} else if (which == email) {

				email_label.setText(emailLabelText);
			}

		}

		public void focusLost(FocusEvent e) {
		}
	}

	private class OkAction extends AbstractAction {

		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent ae) {
			String ea = null;
			// TODO
			ea = email.getText().trim();
			if (ea.indexOf('@') == -1) {
			}

			String pw = new String(password.getPassword());

			if (pw.length() == 0) {
				password_label.setText(passwordLabelErrorText);
				return;
			}

			try {
				// TODO login
				//
				//hide();
				setVisible(false);
				option = APPROVE_OPTION;
				return;
			} catch (Exception e) {
				email_label.setText(emailLabelErrorText);
				password_label.setText(passwordLabelErrorText);
				return;
			}
		}
	}

	protected class CancelAction extends AbstractAction {
		private static final long serialVersionUID = 1L;

		public void actionPerformed(ActionEvent e) {
			//hide();
			setVisible(false);
			option = CANCEL_OPTION;
		}

	}

	public LoginDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		buildUI();
	}


	public LoginDialog(Frame owner, boolean modal) {
		super(owner, modal);
		buildUI();
	}


	public String getEmail() {
		return email.getText().trim();
	}

	public String getPassword() {
		return new String(password.getPassword());
	}

	public void setEmail(String email) {
		this.email.setText(email);
	}

	protected void buildUI() {
		passwordLabelText = "Password:";
		emailLabelText = "Username:";
		passwordLabelErrorText = "<html><font color=>Password:</font></html>";
		emailLabelErrorText = "<html><font color=read>Username:</font></html>";

		JPanel custom = new JPanel();
		custom.setLayout(new BoxLayout(custom, BoxLayout.Y_AXIS));

		password_label = new JLabel(passwordLabelText);
		email_label = new JLabel(emailLabelText);

		JLabel[] labels = { email_label, password_label };
		password = new JPasswordField(16);
		email = new JTextField(16);
		JTextField[] fields = { email, password };

		for (int i = 0; i < fields.length; i++) {
			fields[i].setAutoscrolls(true);
		}

		JPanel panel = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		panel.setLayout(gridbag);

		addLabelTextRows(labels, fields, gridbag, panel);
		
		custom.add(panel);
		custom.add(Box.createRigidArea(VGAP10));

		//

		JPanel buttons = new JPanel();
		buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
		buttons.add(Box.createHorizontalGlue());

		String edit = "Login";

		okay = new JButton(edit);
		buttons.add(okay);
		buttons.add(Box.createRigidArea(HGAP10));

		String can = "Cancel";

		cancel = new JButton(can);
		buttons.add(cancel);
		buttons.add(Box.createHorizontalGlue());

		buttons.setAlignmentX(Component.CENTER_ALIGNMENT);

		custom.add(buttons);
		custom.add(Box.createRigidArea(VGAP10));

		//
		Border border = BorderFactory.createEmptyBorder(10, 10, 10, 10);
		custom.setBorder(border);

		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(custom, BorderLayout.CENTER);

		getRootPane().setDefaultButton(okay);

		pack();
		
		//
		okay.addActionListener(new OkAction());
		cancel.addActionListener(new CancelAction());

		MyFocusListener fl = new MyFocusListener();
		password.addFocusListener(fl);
		email.addFocusListener(fl);
		
		setLocationRelativeTo(getOwner());
		
	}
}
